package com.coffee.oss.strategy.context;

import com.coffee.common.config.AppConfig;
import com.coffee.common.exception.CustomException;
import com.coffee.oss.strategy.FileStorageStrategy;
import com.google.common.collect.Maps;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 文件存储策略上下文
 *
 * @author Kevin
 */
@Component
public class FileStorageContext {

    Map<String, FileStorageStrategy> fileStorageStrategyMap = Maps.newHashMap();

    public FileStorageContext(List<FileStorageStrategy> list) {
        list.forEach(item -> {
            fileStorageStrategyMap.put(item.getStrategyName(), item);
        });
    }

    /**
     * 获取默认文件存储上下文
     *
     * @return
     */
    public FileStorageStrategy getContext() {
        return getContext(AppConfig.getUploadType());
    }

    /**
     * 获取文件存储上下文
     *
     * @param strategyType
     * @return
     */
    public FileStorageStrategy getContext(String strategyType) {
        FileStorageStrategy fileStorageStrategy = fileStorageStrategyMap.get(strategyType.toUpperCase());
        if (Objects.isNull(fileStorageStrategy)) {
            throw new CustomException("文件存储方式暂不支持");
        }
        return fileStorageStrategy;
    }

}
